package com.liaoye.hellocustomview.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import androidx.annotation.NonNull;

import com.liaoye.hellocustomview.R;

public class AnimationSurfaceView extends SurfaceView {

    private SurfaceHolder surfaceHolder;
    private boolean flag = false;
    private Bitmap bgBitmap;
    private float mSurfaceWidth, mSurfaceHeight; //屏幕宽，高
    private int mBitmapPosX; //开始绘制的图片的坐标
    private Canvas mCanvas;
    private Thread thread;

    private static final String TAG = AnimationSurfaceView.class.getSimpleName();

    //背景移动状态
    private enum State {
        LEFT, RIGHT
    }

    //默认向左
    private State state = State.LEFT;
    private final int BITMAP_STEP = 1; //背景画布移动步伐


    public AnimationSurfaceView(Context context) {
        super(context);
        init();
    }


    public AnimationSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public AnimationSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        surfaceHolder = getHolder();
        surfaceHolder.addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(@NonNull SurfaceHolder holder) {
                Log.i(TAG, "surfaceCreated: ");
                flag = true;
                startAnimation();
            }

            @Override
            public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {

            }

            @Override
            public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
                Log.i(TAG, "surfaceDestroyed: ");
                flag = false;
            }
        });
    }

    private void startAnimation() {
        mSurfaceWidth = getWidth();
        mSurfaceHeight = getHeight();

        int minWidth = (int) (mSurfaceWidth * 3 / 2);

        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.scenery);
        bgBitmap = Bitmap.createScaledBitmap(bitmap, minWidth, (int) mSurfaceHeight, true);

        thread = new Thread(() -> {
            while (flag) {
                mCanvas = surfaceHolder.lockCanvas();
                drawView();
                surfaceHolder.unlockCanvasAndPost(mCanvas);
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        thread.start();
    }

    private void drawView() {
        //绘制背景
        mCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); //清空屏幕
        mCanvas.drawBitmap(bgBitmap, mBitmapPosX, 0, null) ; //绘制当前屏幕背景

        switch (state) {
            case LEFT:
                mBitmapPosX -= BITMAP_STEP;
                break;
            case RIGHT:
                mBitmapPosX += BITMAP_STEP;
                break;
            default:
                break;
        }

        if(mBitmapPosX <=  (-mSurfaceWidth / 2)){
            state = State.RIGHT;
        }
        if(mBitmapPosX > 0) {
            state = State.LEFT;
        }
    }






}
